﻿using DevExpress.XtraGrid.Views.Grid;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_ExamRoom_List : BaseForm
    {
        ServiceBase service = new ServiceBase();
        public Base_ExamRoom_List()
        {
            InitializeComponent();
            BindListControl();
        }

        private void BindListControl()
        {//SessionerHelper.ExamPlaceID, SessionerHelper.SpecialityKindsNO, SessionerHelper.ExamLevel
            using (DataTable dt = service.GetExamEntretenimientohTable(SessionerHelper.SystemYear, string.Empty, string.Empty, string.Empty))
            {
                if (dt != null)
                {
                    listBoxEntretenimientoh.DisplayMember = "EntretenimientohName";
                    listBoxEntretenimientoh.ValueMember = "EntretenimientohID";
                    listBoxEntretenimientoh.DataSource = dt;
                }
            }
        }


        private void BindMacData()
        {
            gdMac.DataSource = null;
            using (DataTable dt = service.GetExamMacByRoomTable(SessionerHelper.SystemYear))
            {
                gdMac.DataSource = dt;
            }
        }

        private void listBoxEntretenimientoh_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindRoom();
            this.BindMacData();
            this.BindRoomMacData();
        }

        private void BindRoom()
        {
            using (DataTable dt = service.GetExamRoomTable(listBoxEntretenimientoh.SelectedValue.ToSafeString(), SessionerHelper.SystemYear))
            {
                dgRoom.DataSource = dt;
            }
        }

        private void btnAddRoom_Click(object sender, EventArgs e)
        {
            if (listBoxEntretenimientoh.SelectedItem == null || listBoxEntretenimientoh.SelectedIndex < 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择考点、专业、级别、方式!");
                return;
            }
            string str = listBoxEntretenimientoh.SelectedValue.ToSafeString();
            Base_ExamRoom_Option option = new Base_ExamRoom_Option(str, string.Empty);
            if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BindRoom();
            }
        }

        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hInfo = gridView1.CalcHitInfo(new Point(e.X, e.Y));
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                //判断光标是否在行范围内 
                if (hInfo.InRow)
                {
                    string strRoomID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ExamRoomNO").ToSafeString();
                    Base_ExamRoom_Option option = new Base_ExamRoom_Option(string.Empty, strRoomID);
                    if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        BindRoom();
                    }
                }
            }
        }

        private void BindRoomMacData()
        {
            gdRoomMac.DataSource = null;
            string strRoomMacExamRoomNO = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ExamRoomNO").ToSafeString();
            if (strRoomMacExamRoomNO.Length == 0)
            {
                gdRoomMac.DataSource = null;
            }
            else
            {
                using (DataTable dt = service.GetRoomMacDataTable(strRoomMacExamRoomNO, string.Empty, SessionerHelper.SystemYear))
                {
                    gdRoomMac.DataSource = dt;
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string strEntretenimientohID = listBoxEntretenimientoh.SelectedValue.ToSafeString();
                string strExamRoomNO = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ExamRoomNO").ToSafeString();
                GridRowCollection gridrows = gridView2.SelectedRows;
                if (gridrows != null)
                {
                    List<TB_Exam_ExamRoomMacMapping> list = new List<TB_Exam_ExamRoomMacMapping>();
                    foreach (GridRow grow in gridrows)
                    {
                        string strMacNO = grow.DataRow["MacNO"].ToSafeString();
                        TB_Exam_ExamRoomMacMapping model = new TB_Exam_ExamRoomMacMapping();
                        model.ExamRoomNO = strExamRoomNO;
                        model.ExamYear = SessionerHelper.SystemYear;
                        model.MacNo = strMacNO;
                        list.Add(model);
                    }
                    service.SaveExamRoomMacMapping(list);
                }
                BindMacData();
                BindRoomMacData();
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                GridRowCollection gridrows = gridView3.SelectedRows;
                if (gridrows != null)
                {
                    List<string> list = new List<string>();
                    foreach (GridRow grow in gridrows)
                    {
                        string strRoomMacExamRoomNO = grow.DataRow["ExamRoomNO"].ToSafeString();
                        string strRoomMacMac = grow.DataRow["MacNo"].ToSafeString();
                        string strRoomMacExamYear = grow.DataRow["ExamYear"].ToSafeString();
                        list.Add(strRoomMacExamRoomNO + "," + strRoomMacMac + "," + strRoomMacExamYear);
                    }
                    service.DeleteExamRoomMacMapping(list);
                }
                BindMacData();
                BindRoomMacData();
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 1)
            {
                int rowIndex = e.RowHandle;
                this.BindMacData();
                this.BindRoomMacData();
            }
        }

        private void gridView2_DoubleClick(object sender, EventArgs e)
        {
            if (gridView2.GetFocusedValue() != null)
            {
                GridView rows = sender as GridView;
                int[] inteurm = rows.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    string strMacID = rows.Rows[inteurm[0]].DataRow["MacID"].ToString();
                    Base_Mac_Option option = new Base_Mac_Option(strMacID);
                    if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        BindMacData();
                    }
                }
            }
        }
    }
}
